<?php
/**
 * 按钮组
 * User: ZFC
 * Date: 2015/7/6
 * Time: 11:39
 */

namespace common\widgets;

use yii\helpers\ArrayHelper;
use yii\helpers\Html;

class ButtonGroup extends Widget {


    /*
     * [
     *      'size' => 'btn-sm',
     *      'color' => 'btn-primary
     *      'buttons' => [label, url, icon='', color='', 'show condition']
     * ]
     */

    public $buttons = [];

    //btn-lg btn-sm btn-xs
    public $size = '';

    //btn-default btn-primary btn-success btn-info btn-warning btn-danger btn-link
    public $color = 'btn-primary';


    public function init()
    {
        parent::init();
        Html::addCssClass($this->options, 'btn-group');
    }

    /**
     * Renders the widget.
     */
    public function run()
    {
        return $this->renderButtons();
    }

    /**
     * Generates the buttons that compound the group as specified on [[buttons]].
     * @return string the rendering result.
     */
    protected function renderButtons()
    {
        $buttons = [];
        foreach ($this->buttons as $button) {
            if (is_array($button)) {
                if(!isset($button['condition']) || (isset($button['condition']) && $button['condition']>0)){
                    $label = isset($button['label']) ? $button['label'] : '';
                    //按钮图标
                    if(!empty($button['icon']))
                        $label = Html::tag('i', '', ['class'=>'glyphicon glyphicon-'.$button['icon']]) . ' ' . $label;
                    //按钮尺寸
                    $class = 'btn '.$this->size;
                    //按钮颜色
                    if(!empty($button['color'])){
                        $class = $class . ' ' . $button['color'];
                    }else{
                        $class = $class . ' ' . $this->color;
                    }
                    $buttons[] = Html::a($label, $button['url'], ['class'=>$class]);
                }else{
                    $buttons[] = '';
                }
            } else {
                $buttons[] = $button;
            }
        }

        return implode("\n", $buttons);
    }

}